package com.tanhua.dubbo.api;

import com.tanhua.dubbo.api.mongo.OptionApi;
import com.tanhua.model.mongo.Option;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class OptionApiImpl implements OptionApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查出该试题下的所有选项
     *
     * @param questionId
     * @return
     */
    @Override
    public List<Option> findByQuestionId(String questionId) {
        Query query = new Query(Criteria.where("questionId").is(questionId));
        return mongoTemplate.find(query, Option.class);
    }

    /**
     * 查询所有符合条件的选项
     * @param optionIds
     * @return
     */
    @Override
    public List<Option> findByIds(List<ObjectId> optionIds) {
        Query query=new Query();
        query.addCriteria(Criteria.where("id").in(optionIds));
        return mongoTemplate.find(query, Option.class);
    }

    /**
     * 获取分数集合
     *
     * @param optionIds
     * @return
     */
    @Override
    public List<Double> findScore(List<ObjectId> optionIds) {
        Query query = new Query(Criteria.where("id").in(optionIds));
        List<Option> optionList = mongoTemplate.find(query, Option.class);
        List<Double> result = optionList.stream().map(Option::getScore).collect(Collectors.toList());
        return result;
    }
}
